package com.wc.servicezuul.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Component
public class Myfilter extends ZuulFilter{

    private  static Logger logger= LoggerFactory.getLogger(Myfilter.class);

    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {
        RequestContext rct=RequestContext.getCurrentContext();
        HttpServletRequest httpServletRequest=rct.getRequest();
        logger.info(String.format("%s>>>%s",httpServletRequest.getMethod(),httpServletRequest.getRequestURI()));
        Object token=httpServletRequest.getParameter("token");
        if(token==null){
            logger.warn("token is null");
            rct.setSendZuulResponse(false);
            rct.setResponseStatusCode(401);
            try {
                rct.getResponse().getWriter().write("token si empty~");
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
        logger.info("ok");
        return null;
    }
}
